'use strict';
/**
 * @name: 埋点云函数
 * @description: 埋点云函数
 * @version: 1.0.0
 * @author: hxx
 * @Date: 2020-12-22
 * @LastEditors: hxx
 * @LastEditTime: 2020-12-22
 */

const ctxutil = require('./utildev/exploitInitCtx/initctx/index.js');

// const userAccess = require("./utildev/userAccess/index.js");
const userAccess = require("mf-useraccess");

// pvuv埋点
exports.pvuv = async (ctx) => {
  if(!ctx && !ctx.data.code){
    return {
        success: false,
        data: {},
        errorCode: "1001",
        errorMessage: '项目埋点ID必传!'
      }
  }
  await ctxutil.apply(ctx);

  let _dataTrack = new userAccess.dataTrack(ctx);

  let obj = {
    version: "v2",
    projectCode: ctx.data.code,
    timestamp: new Date().getTime().toString(),
    province: "",
    city: "",
    customType: "",
    origin: "miniapp",
    userid: ctx.userNick || ctx.mixNick,
    type: "flowSource"
  }
  // 存储用户信息
  let res = await _dataTrack.puvu(obj);

  return res;
}

// 自定义埋点
exports.custom = async (ctx) => {
  if(!ctx.data && !ctx.data.code && !ctx.data.customType){
    return {
        success: false,
        data: {},
        errorCode: "1001",
        errorMessage: '项目埋点ID和customType必传!'
      }
  }
  await ctxutil.apply(ctx);

  let _dataTrack = new userAccess.dataTrack(ctx);

  let obj = {
    version: "v2",
    projectCode: ctx.data.code,
    timestamp: new Date().getTime().toString(),
    province: ctx.data.province || "",
    city: ctx.data.city || "",
    customType: ctx.data.customType || "",
    origin: ctx.data.origin || "miniapp",
    userid: ctx.userNick || ctx.mixNick,
    type: ""
  }
  // 存储用户信息
  let res = await _dataTrack.custom(obj);

  return res;
}
